﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using Telerik.Web.UI;
using SalePartial.BLL;
using System.Data;
using SalePartial.Model;
using System.Web.Security;

public partial class NewsCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //修改页头
            Page.Header.Title = String.Format("环球零件资源网--网站管理向导");

            // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                //string radalertscript = "<script language='javascript'>function f(){radalert('Welcome to RadWindow for <strong>ASP.NET AJAX</strong>!', 330, 210); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";

                string radalertscript = "<script language='javascript'>function f(){radopen(\"../../../Login/NoPermission.aspx\", null); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);

                //Response.Redirect(GlobalSetting.UrlHomePage, true);
            }


        }
    }


         private void bindTorep()
        { 
            repCatagory.DataBind();
        }



        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //添加类别
            string caName = txtCaName.Text.Trim();
            XPNewsCateGory cm = new XPNewsCateGory();
            DataSet ds = cm.SelectDynamic("CaName='"+caName+"'",null);

            if (ds!=null && ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('该类别名称已经存在！');</script>");

                return;
            }
            else
            { 
                XPNewsCateGoryInfo info = new XPNewsCateGoryInfo();
                info.CaName = caName;
                info.CreateUserID = new Guid(Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());

                if (cm.Insert(info)==0)
                {
                    txtCaName.Text = "";
                    bindTorep();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('类别添加失败，请联系管理员！');</script>");
                }
            }
        }

        protected void LbtnDel_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            XPNewsCateGory cm = new XPNewsCateGory();
            if (lb.CommandArgument!=null && lb.CommandArgument != string.Empty)
            {
                XPNewsCateGoryInfo info = new XPNewsCateGoryInfo();
                info.CaID = new Guid(lb.CommandArgument);

                try
                {
                    cm.Delete(info);
                }
                catch(Exception )
                {
                
                  Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<Script language='javascript'>alert('删除失败！');</script>");
                   return;
                }

                bindTorep();
                    
            }
        }

}
